package com.hqq.sort;

        import com.hqq.sort.compare.SortType;
        import com.hqq.sort.insert.InsertionSort;
        import com.hqq.sort.insert.ShellSort;
        import com.hqq.sort.select.SelectionSort;
        import org.apache.commons.lang.time.StopWatch;

/**
 * SortCompare
 * 比较直接选择排序和直接插入排序
 * Created by heqianqian on 2017/8/3.
 */
public class SortCompare {

    /**
     * 针对给定输入 为该种排序算法计时
     */
    public static long time(SortType sortTypes, Comparable[] a) {
        long startTime = System.currentTimeMillis();
        StopWatch timer = new StopWatch();
        if (sortTypes == SortType.INSERTION) {
            InsertionSort.sort(a);
        } else if (sortTypes == SortType.SELECTION) {
            SelectionSort.sort(a);
        } else if (sortTypes == SortType.SHELL) {
            ShellSort.sort(a);
        }
        return System.currentTimeMillis() - startTime;
    }

    public static double timeRandomInput(SortType sortType, int N, int T) {
        //将T个长度为N的数组排序
        double total = 0.0;
        Double[] a = new Double[N];
        for (int i = 0; i < T; i++) {
            //进行一次测试[生成一个数组并排序]
            for (int j = 0; j < N; j++) {
                a[j] = Math.random() * 100;
            }
            //计算T次排序的总时间
            total += time(sortType, a);
        }
        return total;
    }

    /**
     * 测试
     */
    public static void main(String[] args) {
        int N = 100;
        int T = 1000;
        //Insertion
        double insertTime = timeRandomInput(SortType.INSERTION, N, T);
        System.out.printf("[%s] Sort [%d] array which length is [%d] cost [%f] ms", SortType.INSERTION.toString(), T, N, insertTime);
        System.out.println();
        //Selection
        double selectTime = timeRandomInput(SortType.SELECTION, N, T);
        System.out.printf("[%s] Sort [%d] array which length is [%d] cost [%f] ms", SortType.SELECTION.toString(), T, N, selectTime);
        System.out.println();
        //Shell
        double shellTime = timeRandomInput(SortType.SHELL, N, T);
        System.out.printf("[%s] Sort [%d] array which length is [%d] cost [%f] ms", SortType.SHELL.toString(), T, N, shellTime);
        System.out.println();
        //Merge
        double mergeTime = timeRandomInput(SortType.MERGE, N, T);
        System.out.printf("[%s] Sort [%d] array which length is [%d] cost [%f] ms", SortType.MERGE.toString(), T, N, mergeTime);
        System.out.println();
        //Quick
        double quickTime = timeRandomInput(SortType.QUICK, N, T);
        System.out.printf("[%s] Sort [%d] array which length is [%d] cost [%f] ms", SortType.QUICK.toString(), T, N, quickTime);
    }
}
